package com.clstu.qqclient.qqframe;

import com.clstu.qqcommon.Message;
import com.clstu.qqcommon.MessageType;
import com.clstu.qqcommon.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * 这是注册窗口的界面
 */
public class RegisterFrame extends JFrame implements ActionListener {

    private JLabel nameLb = new JLabel("请输入姓名");
    private JTextField name = new JTextField(10);
    private JLabel pwdLb = new JLabel("请输入密码");
    private JPasswordField pwd = new JPasswordField(10);
    private JLabel pwdLb2 = new JLabel("请确认密码");
    private JPasswordField pwd2 = new JPasswordField(10);
    private JLabel sexLb = new JLabel("请选择你的性别");
    private JRadioButton sex1 = new JRadioButton("男",true);
    private JRadioButton sex2 = new JRadioButton("女",false);
    private ButtonGroup bgSex = new ButtonGroup();
    private JButton btRegis = new JButton("注册");
    private JButton btExit = new JButton("退出");

    public RegisterFrame(){
        super("注册");
        this.setLayout(new FlowLayout());//流式布局

        add(nameLb);//加入组件
        add(name);
        add(pwdLb);
        add(pwd);
        add(pwdLb2);
        add(pwd2);
        bgSex.add(sex1);
        bgSex.add(sex2);
        add(sexLb);
        add(sex1);
        add(sex2);
        add(btRegis);
        add(btExit);

        setLocation(700,400);//设置窗口
        setSize(260,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);

        btExit.addActionListener(this);//加入监听器
        btRegis.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getSource()==btRegis){//如果按下的注册键

            String pwd = new String(this.pwd.getPassword());
            String pwd2 = new String(this.pwd2.getPassword());
            if(!pwd.equals(pwd2)){
                JOptionPane.showMessageDialog(this,"这两个密码不相同");
                return;
            }

            String id = this.name.getText();
            String sex;
            if(sex1.isSelected()){
                sex = (String) sex1.getText();
            }else {
                sex = (String) sex2.getText();
            }
            Message message = new Message(null, MessageType.MESSAGE_RESIST, new User(id, pwd, sex));
            try {
                //连接服务器               //                    127.0.0.1表示的是自己的主机,因为服务端也是自己的主机
                Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9999);//

                //发送User对象
                ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                oos.writeObject(message);//把User对象通过对象流发送出去

                //接受Message对象,只在这个时候接受一个对象,此后的所有需要接受的对象都由线程来管理
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                Message ms = (Message) ois.readObject();

                //根据Message对象判断注册成功与否
                if(ms.getMesType().equals(MessageType.MESSAGE_RESIST_SUCCEED)){
                    //如果成功
                    JOptionPane.showMessageDialog(this,id+"注册成功");
                    this.dispose();
                    new LoginFrame();
                    socket.close();
                }else {
                    //如果注册失败,关闭socket
                    JOptionPane.showMessageDialog(this,"注册失败,请重试...");
                    socket.close();
                }
            } catch (IOException | ClassNotFoundException ex) {
                ex.printStackTrace();
            }

        }else if(e.getSource() == btExit){//如果按下了退出键
            this.dispose();
            new LoginFrame();
        }

    }
}
